package com.hmkj.admin.controller.setting;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.po.system.Bank;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.system.BankService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 银行配置
 *
 * @author zhaxiaowei
 * @create 2017-06-27 14:33
 **/
@Controller
@RequestMapping(BaseController.BASE_URI)
public class BankJsonController extends GenericController<Integer, Bank> {

    @Resource
    private BankService bankService;
    @Resource
    private RedisService redisService;

    /**
     * 银行列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("bank/json/list")
    @RequiresPermissions("sys:bank:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        return bankService.getDtGridList(dtGridPager);
    }

    /**
     * 添加银行
     *
     * @param bank
     * @return
     */
    @ResponseBody
    @RequestMapping("bank/json/add")
    @RequiresPermissions("sys:bank:save")
    public ResultEntity<Bank> addJson(Bank bank) throws Exception {
        String prefixPath = redisService.get(ConfigNID.IMAGE_SERVER_URL);
        String icon = bank.getIcon().replace(prefixPath, "");
        bank.setIcon(icon);
        try {
            int i = bankService.insert(bank);
            if (i != 1) return fail();
            return ok();
        } catch (Exception e) {
            return fail(e.getMessage());
        }

    }

    /**
     * 编辑银行
     *
     * @param bank
     * @return
     */
    @ResponseBody
    @RequestMapping("bank/json/edit")
    @RequiresPermissions("sys:bank:save")
    public ResultEntity editJson(Bank bank) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        String prefixPath = redisService.get(ConfigNID.IMAGE_SERVER_URL);
        String icon = bank.getIcon().replace(prefixPath, "");
        bank.setIcon(icon);
        try {
            bankService.updateByPrimaryKeySelective(bank);
            resultEntity.setCode(ResultEntity.SUCCESS);
        } catch (Exception e) {
            resultEntity.setMessage(e.getMessage());
            resultEntity.setCode(ResultEntity.FAIL);
        }
        return resultEntity;
    }

    /**
     * 删除银行
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("bank/json/del")
    @RequiresPermissions("sys:bank:del")
    public ResultEntity delJson(int id) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        try {
            Bank bank = new Bank();
            bank.setId(id);
            bank.setDelFlag(Bank.DELFLAG.DELETED.code);
            bankService.updateByPrimaryKeySelective(bank);
            resultEntity.setCode(ResultEntity.SUCCESS);
        } catch (Exception e) {
            resultEntity.setMessage(e.getMessage());
            resultEntity.setCode(ResultEntity.FAIL);
        }
        return resultEntity;
    }
}
